library(foreign)
library(Zelig)
library(survival)
library(stargazer)
library(ggplot2)
library(reshape2)
library(plyr)

#LOAD DATA
imf_sample<-read.dta("G:/MAX-Filer/Collab/dmcdowel-S12/Admin/2nd Projects/IMF - Need for Speed/RIO R&R/data/nfs_master_sample_only.dta")
imf8412_nc<-read.dta("G:/MAX-Filer/Collab/dmcdowel-S12/Admin/2nd Projects/IMF - Need for Speed/RIO R&R/data/nfs_master.dta")
imf8487_nc<-read.dta("G:/MAX-Filer/Collab/dmcdowel-S12/Admin/2nd Projects/IMF - Need for Speed/RIO R&R/data/nfs_master_8487.dta")
imf8812_nc<-read.dta("G:/MAX-Filer/Collab/dmcdowel-S12/Admin/2nd Projects/IMF - Need for Speed/RIO R&R/data/nfs_master_8812.dta")
imf8412_ms<-read.dta("G:/MAX-Filer/Collab/dmcdowel-S12/Admin/2nd Projects/IMF - Need for Speed/RIO R&R/data/MS_replicate.dta")
imf8487_ms<-read.dta("G:/MAX-Filer/Collab/dmcdowel-S12/Admin/2nd Projects/IMF - Need for Speed/RIO R&R/data/MS_replicate_8487.dta")
imf8812_ms<-read.dta("G:/MAX-Filer/Collab/dmcdowel-S12/Admin/2nd Projects/IMF - Need for Speed/RIO R&R/data/MS_replicate_8812.dta")

###DESCRIPTIVE FIGURES###
#HISTOGRAM
ggplot() +
  geom_bar(aes(x = lag),data=imf_sample,alpha = 0.6) +
  xlab(label = 'Loan Approval Period (days)') +
  theme_bw(base_size = 22.0) +
  coord_cartesian(xlim = c(-5,205),ylim = c(0,50))

#DOTPLOT
ggplot() +
  geom_jitter(aes(x = yeara,y = lag),data=imf_sample,shape = 19,alpha = 0.6) +
  theme_bw(base_size = 24.0) +
  ylab(label = 'Loan Approval Period (days)') +
  coord_cartesian(ylim = c(-4,200)) +
  coord_cartesian(ylim = c(-4,200)) +
  geom_smooth(aes(x = yeara,y = lag),data=imf_sample,colour = '#000000',size = 0.3,linetype = 2,method = 'lm') +
  xlab(label = 'Year')

####################################
##############COX MODEL#############
####################################

#FULL COX MODEL
cox.1<-zelig(Surv(lag) ~ gdplog + poplog + pup + rpm + circ + crisis2 + debtcrisis + log_quota_share + polity2 + unsc + g5_idealpointdistance + log_g5_aid_gdp + afr + asia + amer + mena + conlend * log_g5_bank_gdp + y1983 + y1984 + y1985 + y1986 + y1987 + y1988 + y1989 + y1990 + y1991 + y1992 + y1993 + y1994 + y1995 + y1996 + y1997 + y1998 + y1999 + y2000 + y2001 + y2002 + y2003 + y2004 + y2005 + y2006 + y2007 + y2008 + y2009 + y2010 + y2011, model="coxph", robust=TRUE, cluster="country", method="efron", data=imf8412_nc)

#CONCERTED LENDING YEARS COX MODEL
cox.2<-zelig(Surv(lag) ~ gdplog + poplog + pup + rpm + crisis2 + debtcrisis + polity2 + log_quota_share + unsc + g5_idealpointdistance + log_g5_aid_gdp + log_debtserv_exp * log_g5_bank_gdp + y1983 + y1984 + y1985 + y1986 , model="coxph", robust=TRUE, cluster="country", method="efron", data=imf8487_nc)

#POST-CONCERTED LENDING YEARS COX MODELS
cox.3<-zelig(Surv(lag) ~ gdplog + poplog + pup + rpm + circ + crisis2 + debtcrisis + polity2 + log_quota_share + unsc + g5_idealpointdistance + log_g5_aid_gdp + afr + asia + amer + mena + log_pct_bond * log_g5_bank_gdp + y1987 + y1988 + y1989 + y1990 + y1991 + y1992 + y1993 + y1994 + y1995 + y1996 + y1997 + y1998 + y1999 + y2000 + y2001 + y2002 + y2003 + y2004 + y2005 + y2006 + y2007 + y2008 + y2009 + y2010 + y2011, model="coxph", robust=TRUE, cluster="country", method="efron", data=imf8812_nc)

cox.4<-zelig(Surv(lag) ~ gdplog + poplog + pup + rpm + circ + crisis2 + debtcrisis + polity2 + log_quota_share + unsc + g5_idealpointdistance + log_g5_aid_gdp + afr + asia + amer + mena + log_stdebt_totdebt * log_g5_bank_gdp + y1987 + y1988 + y1989 + y1990 + y1991 + y1992 + y1993 + y1994 + y1995 + y1996 + y1997 + y1998 + y1999 + y2000 + y2001 + y2002 + y2003 + y2004 + y2005 + y2006 + y2007 + y2008 + y2009 + y2010 + y2011, model="coxph", robust=TRUE, cluster="country", method="efron", data=imf8812_nc)

summary(cox.1)
summary(cox.2)
summary(cox.3)
summary(cox.4)

########################################
###NEGATIVE BINOMIAL ROBUSTNESS CHECK###
########################################

#FULL NEGBIN MODEL
negbin.1<-zelig(lag ~ gdplog + poplog + pup + rpm + circ + crisis2 + debtcrisis + polity2 + log_quota_share + unsc + g5_idealpointdistance + log_g5_aid_gdp + afr + asia + amer + mena + conlend * log_g5_bank_gdp + y1983 + y1984 + y1985 + y1987 + y1988 + y1989 + y1990 + y1991 + y1992 + y1993 + y1995 + y1996 + y1997 + y1998 + y1999 + y2000 + y2001 + y2002 + y2003 + y2004 + y2005 + y2006 + y2007 + y2008 + y2009 + y2010, model="negbin", robust=TRUE, data=imf8412_nc)

#CONCERTED LENDING YEARS NEGBIN MODEL
negbin.2<-zelig(lag ~ gdplog + poplog + pup + rpm + crisis2 + debtcrisis + polity2 + log_quota_share + unsc + g5_idealpointdistance + log_g5_aid_gdp + afr + asia + amer + log_debtserv_exp * log_g5_bank_gdp + y1983 + y1984 + y1985, model="negbin", robust=TRUE, data=imf8487_nc)

#POST-CONCERTED LENDING YEARS NEGBIN MODELS
negbin.3<-zelig(lag ~ gdplog + poplog + pup + rpm + circ + crisis2 + debtcrisis + polity2 + log_quota_share + unsc + g5_idealpointdistance + log_g5_aid_gdp + afr + asia + amer + mena + log_pct_bond * log_g5_bank_gdp + y1987 + y1988 + y1989 + y1990 + y1991 + y1992 + y1993 + y1995 + y1996 + y1997 + y1998 + y1999 + y2000 + y2001 + y2002 + y2003 + y2004 + y2005 + y2006 + y2007 + y2008 + y2009, model="negbin", robust=TRUE, data=imf8812_nc)

negbin.4<-zelig(lag ~ gdplog + poplog + pup + rpm + circ + crisis2 + debtcrisis + polity2 + log_quota_share + unsc + g5_idealpointdistance + log_g5_aid_gdp + afr + asia + amer + mena + log_stdebt_totdebt * log_g5_bank_gdp + y1987 + y1988 + y1989 + y1990 + y1991 + y1992 + y1993 + y1995 + y1996 + y1997 + y1998 + y1999 + y2000 + y2001 + y2002 + y2003 + y2004 + y2005 + y2006 + y2007 + y2008 + y2009, model="negbin", robust=TRUE, data=imf8812_nc)

summary(negbin.1)
summary(negbin.2)
summary(negbin.3)
summary(negbin.4)

###############################################################
###########REPLICATE MODY AND SARAVIA'S MEASURE################
###############################################################

#FULL COX MODEL WITH M&S DATA
cox.ms.1<-zelig(Surv(ms_lag) ~ gdplog + poplog + pup + rpm + circ + crisis2 + debtcrisis + log_quota_share + polity2 + unsc + g5_idealpointdistance + log_g5_aid_gdp + afr + asia + amer + mena + conlend * log_g5_bank_gdp + y1983 + y1984 + y1985 + y1986 + y1987 + y1988 + y1989 + y1990 + y1991 + y1992 + y1993 + y1994 + y1995 + y1996 + y1997 + y1998 + y1999 + y2000 + y2001 + y2003 + y2004 + y2005 + y2006 + y2008, model="coxph", robust=TRUE, cluster="country", method="efron", data=imf8412_ms)

#CONCERTED LENDING YEARS COX MODEL WITH M&S DATA
cox.ms.2<-zelig(Surv(ms_lag) ~ gdplog + poplog + pup + rpm + crisis2 + debtcrisis + polity2 + log_quota_share + unsc + g5_idealpointdistance + log_g5_aid_gdp + log_debtserv_exp * log_g5_bank_gdp + y1983 + y1984 + y1985 + y1986 , model="coxph", robust=TRUE, cluster="country", method="efron", data=imf8487_ms)

#POST-CONCERTED LENDING YEARS COX MODEL WITH M&S DATA
cox.ms.3<-zelig(Surv(ms_lag) ~ gdplog + poplog + pup + rpm + circ + crisis2 + debtcrisis + polity2 + log_quota_share + unsc + g5_idealpointdistance + log_g5_aid_gdp + afr + asia + amer + mena + log_pct_bond * log_g5_bank_gdp + y1987 + y1988 + y1989 + y1990 + y1991 + y1992 + y1993 + y1994 + y1995 + y1996 + y1997 + y1998 + y1999 + y2000 + y2001 + y2003 + y2004, model="coxph", robust=TRUE, cluster="country", method="efron", data=imf8812_ms)

cox.ms.4<-zelig(Surv(ms_lag) ~ gdplog + poplog + pup + rpm + circ + crisis2 + debtcrisis + polity2 + log_quota_share + unsc + g5_idealpointdistance + log_g5_aid_gdp + afr + asia + amer + mena + log_stdebt_totdebt * log_g5_bank_gdp + y1987 + y1988 + y1989 + y1990 + y1991 + y1992 + y1993 + y1994 + y1995 + y1996 + y1997 + y1998 + y1999 + y2000 + y2001 + y2003 + y2004, model="coxph", robust=TRUE, cluster="country", method="efron", data=imf8812_ms)

summary(cox.ms.1)
summary(cox.ms.2)
summary(cox.ms.3)
summary(cox.ms.4)





#################POSTESTIMATION##################



###########################COX##########
##################POSTESTIMATION########
##################FULL MODEL############
########################################
########################################

# log_g5_bank_gdp summary statistics if yeara<1988
# SD     2.222303
# -1sd -25.772213
# mean -23.54991
# +1sd -21.327607

#log_g5_bank_gdp summary statistics if yeara>1987
# SD     2.126896
# -1sd -25.143336
# mean -23.01644
# +1sd -20.889544


x.lo.cox.1.bank.1     <-setx(cox.1, log_g5_bank_gdp= -25.772213, conlend=1)
x.hi.cox.1.bank.1     <-setx(cox.1, log_g5_bank_gdp= -21.327607, conlend=1)

s.cox.1.bank.11 <-sim(cox.1, x=x.lo.cox.1.bank.1)
s.cox.1.bank.12 <-sim(cox.1, x=x.hi.cox.1.bank.1)

#create objects for survival at high/low inlinethg5a
surv_cox.1.bank.1_lo <- s.cox.1.bank.11$qi$survival
surv_cox.1.bank.1_hi <- s.cox.1.bank.12$qi$survival

#create empty vectors
surv_cox.1.bank.1_lo.vec  <-NULL
surv_cox.1.bank.1_hi.vec  <-NULL

#fill vector with survival estimates and 95% CI
#g5aid at mean, kaopen at -1 SD
for(i in 1:84){
  surv_cox.1.bank.1_lo.out <-quantile(surv_cox.1.bank.1_lo[,i],probs=c(.5))
  surv_cox.1.bank.1_lo.vec <-rbind(surv_cox.1.bank.1_lo.vec, surv_cox.1.bank.1_lo.out) 
}
#g5aid at +2 SD, kaopen at -1SD
for(i in 1:84){
  surv_cox.1.bank.1_hi.out <-quantile(surv_cox.1.bank.1_hi[,i],probs=c(.5))
  surv_cox.1.bank.1_hi.vec <-rbind(surv_cox.1.bank.1_hi.vec, surv_cox.1.bank.1_hi.out) 
}

#write survival estimates to csv
write.csv(surv_cox.1.bank.1_lo.vec, "surv_cox.1.bank.1_lo.csv")
write.csv(surv_cox.1.bank.1_hi.vec, "surv_cox.1.bank.1_hi.csv")

#create first-differences matrices
fdmatrix_cox.1.bank.1 <- (surv_cox.1.bank.1_hi - surv_cox.1.bank.1_lo)

#create empty vector
fd.vec_cox.1.bank.1 <-NULL

#fill vectors with first difference estimate and 95% CI
for(i in 1:84){
  fd.out_cox.1.bank.1 <- quantile(fdmatrix_cox.1.bank.1 [,i],probs=c(.025,.05,.5,.95,.975))
  fd.vec_cox.1.bank.1 <- rbind(fd.vec_cox.1.bank.1, fd.out_cox.1.bank.1) 
}

#Write matrix to .csv
write.csv(fd.vec_cox.1.bank.1, "fd.vec_cox.1.bank.1.csv")

#POST-CL SIMULATION
x.lo.cox.1.bank.2     <-setx(cox.1, log_g5_bank_gdp= -25.143336, conlend=0) 
x.hi.cox.1.bank.2     <-setx(cox.1, log_g5_bank_gdp= -20.889544, conlend=0) 

s.cox.1.bank.21 <-sim(cox.1, x=x.lo.cox.1.bank.2)
s.cox.1.bank.22 <-sim(cox.1, x=x.hi.cox.1.bank.2)

#create objects for survival at high/low inlinethg5a
surv_cox.1.bank.2_lo <- s.cox.1.bank.21$qi$survival
surv_cox.1.bank.2_hi <- s.cox.1.bank.22$qi$survival

#create empty vectors
surv_cox.1.bank.2_lo.vec  <-NULL
surv_cox.1.bank.2_hi.vec  <-NULL

#fill vector with survival estimates and 95% CI
#g5aid at mean, kaopen at -1 SD
for(i in 1:84){
  surv_cox.1.bank.2_lo.out <-quantile(surv_cox.1.bank.2_lo[,i],probs=c(.5))
  surv_cox.1.bank.2_lo.vec <-rbind(surv_cox.1.bank.2_lo.vec, surv_cox.1.bank.2_lo.out) 
}
#g5aid at +2 SD, kaopen at -1SD
for(i in 1:84){
  surv_cox.1.bank.2_hi.out <-quantile(surv_cox.1.bank.2_hi[,i],probs=c(.5))
  surv_cox.1.bank.2_hi.vec <-rbind(surv_cox.1.bank.2_hi.vec, surv_cox.1.bank.2_hi.out) 
}

#write survival estimates to csv
write.csv(surv_cox.1.bank.2_lo.vec, "surv_cox.1.bank.2_lo.csv")
write.csv(surv_cox.1.bank.2_hi.vec, "surv_cox.1.bank.2_hi.csv")

#create first-differences matrices
fdmatrix_cox.1.bank.2 <- (surv_cox.1.bank.2_hi - surv_cox.1.bank.2_lo)

#create empty vector
fd.vec_cox.1.bank.2 <-NULL

#fill vectors with first difference estimate and 95% CI
for(i in 1:84){
  fd.out_cox.1.bank.2 <- quantile(fdmatrix_cox.1.bank.2 [,i],probs=c(.025,.05,.5,.95,.975))
  fd.vec_cox.1.bank.2 <- rbind(fd.vec_cox.1.bank.2, fd.out_cox.1.bank.2) 
}

#Write matrix to .csv
write.csv(fd.vec_cox.1.bank.2, "fd.vec_cox.1.bank.2.csv")


####################################################
#################CONCERTED LENDING YEARS############
###BANK EXPOSURE * DEBT SERVICE SIMULATIONS########
#####################################################

x.lo.cox.2.bank.1     <-setx(cox.2, log_g5_bank_gdp= -25.772213, log_debtserv_exp=2.7721551)
x.hi.cox.2.bank.1     <-setx(cox.2, log_g5_bank_gdp= -21.327607, log_debtserv_exp=2.7721551)

s.cox.2.bank.11 <-sim(cox.2, x=x.lo.cox.2.bank.1)
s.cox.2.bank.12 <-sim(cox.2, x=x.hi.cox.2.bank.1)

#create objects for survival at high/low inlinethg5a
surv_cox.2.bank.1_lo <- s.cox.2.bank.11$qi$survival
surv_cox.2.bank.1_hi <- s.cox.2.bank.12$qi$survival

#create empty vectors
surv_cox.2.bank.1_lo.vec  <-NULL
surv_cox.2.bank.1_hi.vec  <-NULL

#fill vector with survival estimates and 95% CI
#g5aid at mean, kaopen at -1 SD
for(i in 1:41){
  surv_cox.2.bank.1_lo.out <-quantile(surv_cox.2.bank.1_lo[,i],probs=c(.5))
  surv_cox.2.bank.1_lo.vec <-rbind(surv_cox.2.bank.1_lo.vec, surv_cox.2.bank.1_lo.out) 
}
#g5aid at +2 SD, kaopen at -1SD
for(i in 1:41){
  surv_cox.2.bank.1_hi.out <-quantile(surv_cox.2.bank.1_hi[,i],probs=c(.5))
  surv_cox.2.bank.1_hi.vec <-rbind(surv_cox.2.bank.1_hi.vec, surv_cox.2.bank.1_hi.out) 
}

#write survival estimates to csv
write.csv(surv_cox.2.bank.1_lo.vec, "surv_cox.2.bank.1_lo.csv")
write.csv(surv_cox.2.bank.1_hi.vec, "surv_cox.2.bank.1_hi.csv")

#create first-differences matrices
fdmatrix_cox.2.bank.1 <- (surv_cox.2.bank.1_hi - surv_cox.2.bank.1_lo)

#create empty vector
fd.vec_cox.2.bank.1 <-NULL

#fill vectors with first difference estimate and 95% CI
for(i in 1:41){
  fd.out_cox.2.bank.1 <- quantile(fdmatrix_cox.2.bank.1 [,i],probs=c(.025,.05,.5,.95,.975))
  fd.vec_cox.2.bank.1 <- rbind(fd.vec_cox.2.bank.1, fd.out_cox.2.bank.1) 
}

#Write matrix to .csv
write.csv(fd.vec_cox.2.bank.1, "fd.vec_cox.2.bank.1.csv")

#POST-CL SIMULATION
x.lo.cox.2.bank.2     <-setx(cox.2, log_g5_bank_gdp= -25.772213, log_debtserv_exp=3.8021189) 
x.hi.cox.2.bank.2     <-setx(cox.2, log_g5_bank_gdp= -21.327607, log_debtserv_exp=3.8021189) 

s.cox.2.bank.21 <-sim(cox.2, x=x.lo.cox.2.bank.2)
s.cox.2.bank.22 <-sim(cox.2, x=x.hi.cox.2.bank.2)

#create objects for survival at high/low inlinethg5a
surv_cox.2.bank.2_lo <- s.cox.2.bank.21$qi$survival
surv_cox.2.bank.2_hi <- s.cox.2.bank.22$qi$survival

#create empty vectors
surv_cox.2.bank.2_lo.vec  <-NULL
surv_cox.2.bank.2_hi.vec  <-NULL

#fill vector with survival estimates and 95% CI
#g5aid at mean, kaopen at -1 SD
for(i in 1:41){
  surv_cox.2.bank.2_lo.out <-quantile(surv_cox.2.bank.2_lo[,i],probs=c(.5))
  surv_cox.2.bank.2_lo.vec <-rbind(surv_cox.2.bank.2_lo.vec, surv_cox.2.bank.2_lo.out) 
}
#g5aid at +2 SD, kaopen at -1SD
for(i in 1:41){
  surv_cox.2.bank.2_hi.out <-quantile(surv_cox.2.bank.2_hi[,i],probs=c(.5))
  surv_cox.2.bank.2_hi.vec <-rbind(surv_cox.2.bank.2_hi.vec, surv_cox.2.bank.2_hi.out) 
}

#write survival estimates to csv
write.csv(surv_cox.2.bank.2_lo.vec, "surv_cox.2.bank.2_lo.csv")
write.csv(surv_cox.2.bank.2_hi.vec, "surv_cox.2.bank.2_hi.csv")

#create first-differences matrices
fdmatrix_cox.2.bank.2 <- (surv_cox.2.bank.2_hi - surv_cox.2.bank.2_lo)

#create empty vector
fd.vec_cox.2.bank.2 <-NULL

#fill vectors with first difference estimate and 95% CI
for(i in 1:41){
  fd.out_cox.2.bank.2 <- quantile(fdmatrix_cox.2.bank.2 [,i],probs=c(.025,.05,.5,.95,.975))
  fd.vec_cox.2.bank.2 <- rbind(fd.vec_cox.2.bank.2, fd.out_cox.2.bank.2) 
}

#Write matrix to .csv
write.csv(fd.vec_cox.2.bank.2, "fd.vec_cox.2.bank.2.csv")




#############################################
#########POST-CONCERTED LENDING MODELS#######
###SHORT-TERM DEBT*BANK EXPOSURE SIMULATION###########
#############################################

#log_g5_bank_gdp summary statistics if yeara>1987
# SD     2.126896
# -1sd -25.143336
# mean -23.01644
# +1sd -20.889544

x.lo.cox.4.bank.1     <-setx(cox.4, log_g5_bank_gdp= -25.088354, log_stdebt_totdebt=1.8125656)
x.hi.cox.4.bank.1     <-setx(cox.4, log_g5_bank_gdp= -20.847206, log_stdebt_totdebt=1.8125656)


s.cox.4.bank.11 <-sim(cox.4, x=x.lo.cox.4.bank.1)
s.cox.4.bank.12 <-sim(cox.4, x=x.hi.cox.4.bank.1)

#create objects for survival at high/low inlinethg5a
surv_cox.4.bank.1_lo <- s.cox.4.bank.11$qi$survival
surv_cox.4.bank.1_hi <- s.cox.4.bank.12$qi$survival

#create empty vectors
surv_cox.4.bank.1_lo.vec  <-NULL
surv_cox.4.bank.1_hi.vec  <-NULL

#fill vector with survival estimates and 95% CI
#g5aid at mean, kaopen at -1 SD
for(i in 1:61){
  surv_cox.4.bank.1_lo.out <-quantile(surv_cox.4.bank.1_lo[,i],probs=c(.5))
  surv_cox.4.bank.1_lo.vec <-rbind(surv_cox.4.bank.1_lo.vec, surv_cox.4.bank.1_lo.out) 
}
#g5aid at +2 SD, kaopen at -1SD
for(i in 1:61){
  surv_cox.4.bank.1_hi.out <-quantile(surv_cox.4.bank.1_hi[,i],probs=c(.5))
  surv_cox.4.bank.1_hi.vec <-rbind(surv_cox.4.bank.1_hi.vec, surv_cox.4.bank.1_hi.out) 
}

#write survival estimates to csv
write.csv(surv_cox.4.bank.1_lo.vec, "surv_cox.4.bank.1_lo.csv")
write.csv(surv_cox.4.bank.1_hi.vec, "surv_cox.4.bank.1_hi.csv")

#create first-differences matrices
fdmatrix_cox.4.bank.1 <- (surv_cox.4.bank.1_hi - surv_cox.4.bank.1_lo)

#create empty vector
fd.vec_cox.4.bank.1 <-NULL

#fill vectors with first difference estimate and 95% CI
for(i in 1:61){
  fd.out_cox.4.bank.1 <- quantile(fdmatrix_cox.4.bank.1 [,i],probs=c(.025,.05,.5,.95,.975))
  fd.vec_cox.4.bank.1 <- rbind(fd.vec_cox.4.bank.1, fd.out_cox.4.bank.1) 
}

#Write matrix to .csv
write.csv(fd.vec_cox.4.bank.1, "fd.vec_cox.4.bank.1.csv")

#POST-CL SIMULATION
x.lo.cox.4.bank.2     <-setx(cox.4, log_g5_bank_gdp= -25.088354, log_stdebt_totdebt=3.2588804) 
x.hi.cox.4.bank.2     <-setx(cox.4, log_g5_bank_gdp= -20.847206, log_stdebt_totdebt=3.2588804) 

s.cox.4.bank.21 <-sim(cox.4, x=x.lo.cox.4.bank.2)
s.cox.4.bank.22 <-sim(cox.4, x=x.hi.cox.4.bank.2)

#create objects for survival at high/low inlinethg5a
surv_cox.4.bank.2_lo <- s.cox.4.bank.21$qi$survival
surv_cox.4.bank.2_hi <- s.cox.4.bank.22$qi$survival

#create empty vectors
surv_cox.4.bank.2_lo.vec  <-NULL
surv_cox.4.bank.2_hi.vec  <-NULL

#fill vector with survival estimates and 95% CI
#g5aid at mean, kaopen at -1 SD
for(i in 1:61){
  surv_cox.4.bank.2_lo.out <-quantile(surv_cox.4.bank.2_lo[,i],probs=c(.5))
  surv_cox.4.bank.2_lo.vec <-rbind(surv_cox.4.bank.2_lo.vec, surv_cox.4.bank.2_lo.out) 
}
#g5aid at +2 SD, kaopen at -1SD
for(i in 1:61){
  surv_cox.4.bank.2_hi.out <-quantile(surv_cox.4.bank.2_hi[,i],probs=c(.5))
  surv_cox.4.bank.2_hi.vec <-rbind(surv_cox.4.bank.2_hi.vec, surv_cox.4.bank.2_hi.out) 
}

#write survival estimates to csv
write.csv(surv_cox.4.bank.2_lo.vec, "surv_cox.4.bank.2_lo.csv")
write.csv(surv_cox.4.bank.2_hi.vec, "surv_cox.4.bank.2_hi.csv")

#create first-differences matrices
fdmatrix_cox.4.bank.2 <- (surv_cox.4.bank.2_hi - surv_cox.4.bank.2_lo)

#create empty vector
fd.vec_cox.4.bank.2 <-NULL

#fill vectors with first difference estimate and 95% CI
for(i in 1:61){
  fd.out_cox.4.bank.2 <- quantile(fdmatrix_cox.4.bank.2 [,i],probs=c(.025,.05,.5,.95,.975))
  fd.vec_cox.4.bank.2 <- rbind(fd.vec_cox.4.bank.2, fd.out_cox.4.bank.2) 
}

#Write matrix to .csv
write.csv(fd.vec_cox.4.bank.2, "fd.vec_cox.4.bank.2.csv")







##############################
#####NEGBIN POSTESTIMATION####
##############################

x.lo.bank.1.1     <-setx(negbin.1, log_g5_bank_gdp= -25.772213, conlend=1)
x.hi.bank.1.1     <-setx(negbin.1, log_g5_bank_gdp= -21.327607, conlend=1)
s.bank.1.11<-sim(negbin.1, x=x.lo.bank.1.1)
s.bank.1.12<-sim(negbin.1, x=x.hi.bank.1.1)
x.lo.bank.1.2     <-setx(negbin.1, log_g5_bank_gdp= -25.143336, conlend=0)
x.hi.bank.1.2     <-setx(negbin.1, log_g5_bank_gdp= -20.889544, conlend=0)
s.bank.1.21<-sim(negbin.1, x=x.lo.bank.1.2)
s.bank.1.22<-sim(negbin.1, x=x.hi.bank.1.2)

summary(s.bank.1.11)
summary(s.bank.1.12)
summary(s.bank.1.21)
summary(s.bank.1.22)

x.lo.bank.2.1     <-setx(negbin.2, log_g5_bank_gdp= -25.772213, log_debtserv_exp=2.7721551)
x.hi.bank.2.1     <-setx(negbin.2, log_g5_bank_gdp= -21.327607, log_debtserv_exp=2.7721551)
s.bank.2.11<-sim(negbin.2, x=x.lo.bank.2.1)
s.bank.2.12<-sim(negbin.2, x=x.hi.bank.2.1)
x.lo.bank.2.2     <-setx(negbin.2, log_g5_bank_gdp= -25.772213, log_debtserv_exp=3.8021189)
x.hi.bank.2.2     <-setx(negbin.2, log_g5_bank_gdp= -21.327607, log_debtserv_exp=3.8021189)
s.bank.2.21<-sim(negbin.2, x=x.lo.bank.2.2)
s.bank.2.22<-sim(negbin.2, x=x.hi.bank.2.2)

summary(s.bank.2.11)
summary(s.bank.2.12)
summary(s.bank.2.21)
summary(s.bank.2.22)



x.lo.bank.4.1     <-setx(negbin.4, log_g5_bank_gdp= -25.088354, log_stdebt_totdebt=1.8125656)
x.hi.bank.4.1     <-setx(negbin.4, log_g5_bank_gdp= -20.847206, log_stdebt_totdebt=1.8125656)
s.bank.4.11<-sim(negbin.4, x=x.lo.bank.4.1)
s.bank.4.12<-sim(negbin.4, x=x.hi.bank.4.1)
x.lo.bank.4.2     <-setx(negbin.4, log_g5_bank_gdp= -25.088354, log_stdebt_totdebt=3.2588804)
x.hi.bank.4.2     <-setx(negbin.4, log_g5_bank_gdp= -20.847206, log_stdebt_totdebt=3.2588804)
s.bank.4.21<-sim(negbin.4, x=x.lo.bank.4.2)
s.bank.4.22<-sim(negbin.4, x=x.hi.bank.4.2)

summary(s.bank.4.11)
summary(s.bank.4.12)
summary(s.bank.4.21)
summary(s.bank.4.22)

